//
//  MineInfoEditNewViewController.m
//  MJApp
//
//  Created by 王亮 on 15/7/23.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineInfoEditNewViewController.h"
#import "MineInfoEditNewCollectionViewCell.h"
#import "NSDictionary+Category.h"

@interface MineInfoEditNewViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)NSDictionary *userInfoDic;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSArray *addrList;

@end

@implementation MineInfoEditNewViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self obtaionData];
    [self obationAddrList];
    [self UI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)obtaionData {
    
    NSDictionary *dic = @{@"user_id":[userInfoTool share].ID};
    [WLHttpRequestAfter userCenterInfoWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        if ([resultDictionary[@"status"] integerValue] == 1) {
            
            self.userInfoDic = [resultDictionary[@"data"][@"user"] nullValueToEmpty];
            [self.collectionView reloadData];
        }else {
            
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:resultDictionary[@"msg"]];
        }
        
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}


- (void)obationAddrList {
    
    [WLHttpRequestAfter addressListWithParam:nil usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        
        
        if ([resultDictionary[@"status"] integerValue] == 1) {
            
            NSData *jsonData = [resultDictionary[@"data"] dataUsingEncoding:NSUTF8StringEncoding];
            NSArray *arrayList = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
            self.addrList = arrayList;
            [self.collectionView reloadData];
        }
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

- (void)UI {
    
    [self.leftBtn setImage:nil forState:UIControlStateNormal];
    [self.leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.leftBtn setTitleColor:Color_Top_Text forState:UIControlStateNormal];
    [self.rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    [self.rightBtn setTitleColor:Color_Top_Text forState:UIControlStateNormal];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.sectionInset = UIEdgeInsetsMake(22, 0, 22, 0);
    CGRect frame = self.view.bounds;
    frame.size.height -= 64;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
    [self.view addSubview:collectionView];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    self.collectionView = collectionView;
    [collectionView registerClass:[MineInfoEditNewCollectionViewCell class] forCellWithReuseIdentifier:@"MineInfoEditNewCollectionViewCell"];
    collectionView.backgroundColor = Color_Back_Gray;

}

- (void)clickRightButton {
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    MineInfoEditNewCollectionViewCell *cell = (MineInfoEditNewCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:cell.valueDic];
    dic[@"sex"] = [dic[@"sex"] isEqual:@"男"] ? @"1" : @"2";
    dic[@"user_id"] = [userInfoTool share].ID;
    [WLHttpRequestAfter userEditUserInfoWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        
        if ([resultDictionary[@"status"] integerValue] == 1) {
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        
    } andFailureBlock:^(NSError *resultError) {
        
    }];

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MineInfoEditNewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MineInfoEditNewCollectionViewCell" forIndexPath:indexPath];

        
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.userInfoDic];
        if (self.addrList.count > 0) {
            
            dic[@"areaList"] = self.addrList;
            cell.infoDic = dic;
        }
    

    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGSize size = CGSizeZero;
    CGFloat rate = (8 * 80) / 562.0f;
    size.width = IOS_IPHONE_WINDOW_WIDE - 2 * 15;
    size.height = size.width * rate;
    return size;
}


@end
